package com.gzhu.knowledgeAdmin.controller;


import com.gzhu.knowledgeAdmin.common.annotation.MyLog;
import com.gzhu.knowledgeAdmin.common.dto.*;
import com.gzhu.knowledgeAdmin.common.dto.templateFlowDiagram.EdgeAddRequest;
import com.gzhu.knowledgeAdmin.common.dto.templateFlowDiagram.EdgeDeleteRequest;
import com.gzhu.knowledgeAdmin.common.exception.BusinessException;
import com.gzhu.knowledgeAdmin.common.result.ErrorCode;
import com.gzhu.knowledgeAdmin.common.result.R;
import com.gzhu.knowledgeAdmin.common.vo.ParamVO;
import com.gzhu.knowledgeAdmin.common.vo.TemplateExternalVO;
import com.gzhu.knowledgeAdmin.common.vo.TemplateVO;
import com.gzhu.knowledgeAdmin.common.vo.templateFlowDiagram.TemplateFlowDiagramVO;
import com.gzhu.knowledgeAdmin.entity.Template;
import com.gzhu.knowledgeAdmin.parammanage.RecorderParamCRUD;
import com.gzhu.knowledgeAdmin.service.TemplateService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping
public class TemplateController {

    @Resource
    private TemplateService templateService;

    @Resource
    private RecorderParamCRUD recorderParamCRUD;

    @PostMapping("/template")
    public R getTemplate(@RequestBody TemplateGetRequest templateGetRequest){
        Integer templateId = templateGetRequest.getTemplateId();
        if (templateId == null || templateId <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"请求参数错误");
        }

        Template template = templateService.getTemplate(templateGetRequest);

        // 根据录音员名称，填充录音员标号
        List<TemplateExternalVO> templateExternalVOList = recorderParamCRUD.fillRecorderParam(Arrays.asList(template));

        return R.ok(templateExternalVOList.get(0));
    }

    @PostMapping("/template/getBeginTemplate")
    public R getBeginTemplate(@RequestBody BeginTemplateGetRequest beginTemplateGetRequest){

        List<TemplateExternalVO> beginTemplateList = templateService.getBeginTemplate(beginTemplateGetRequest);

        return R.ok(beginTemplateList);
    }

    @GetMapping("/template/getTemplateNameList")
    public R getTemplateNameList(){
        List<ParamVO> templateNameList = templateService.getTemplateNameList();

        return R.ok(templateNameList);
    }

    @GetMapping("/template/getTemplateFlowDiagram")
    public R getTemplateFlowDiagram(@RequestParam("id") Integer id){
        TemplateFlowDiagramVO templateFlowDiagram = templateService.getTemplateFlowDiagram(id);
        return R.ok(templateFlowDiagram);
    }

    @GetMapping("/template/getById")
    public R getTemplateById(@RequestParam("id") Integer id) {
        TemplateVO templateVO = templateService.getTemplateById(id);
        return R.ok(templateVO);
    }

    @MyLog(title = "话术管理模块",optParam = "修改或保存话术节点")
    @PostMapping("/template/flowDiagram/update")
    public R updateTemplateInFlowDiagram(@RequestBody TemplateUpdateRequest templateUpdateRequest) {
        Integer templateId = templateService.updateTemplateInFlowDiagram(templateUpdateRequest);
        return R.ok(templateId);
    }

    @MyLog(title = "话术管理模块",optParam = "删除话术节点")
    @PostMapping("/template/flowDiagram/deleteNode")
    public R deleteFlowDiagramNode(@RequestBody TemplateDeleteRequest templateDeleteRequest){
        templateService.deleteFlowDiagramNode(templateDeleteRequest);

        return R.ok(true);
    }

    @MyLog(title = "话术管理模块",optParam = "新增或修改节点边")
    @PostMapping("/template/flowDiagram/addEdge")
    public R addFlowDiagramEdge(@RequestBody EdgeAddRequest edgeAddRequest) {
        templateService.addFlowDiagramEdge(edgeAddRequest);
        return R.ok();
    }

    @MyLog(title = "话术管理模块",optParam = "删除节点边")
    @PostMapping("/template/flowDiagram/deleteEdge")
    public R deleteFlowDiagramEdge(@RequestBody EdgeDeleteRequest edgeDeleteRequest) {
        templateService.deleteFlowDiagramEdge(edgeDeleteRequest);
        return R.ok();
    }

    @GetMapping("/template/variable/{id}")
    public R getTemplateVariables(@PathVariable("id") Integer id) {

        if (id == null || id < 1) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }

        List<String> variables = templateService.getTemplateVariablesById(id);

        return R.ok(variables);
    }

    @PostMapping("/template/cache")
    public R cacheAllTemplate() {

        templateService.cacheAllTemplate();

        return R.ok(true);
    }

    @MyLog(title = "话术管理模块",optParam = "查询话术")
    @PostMapping("/template/list")
    public R getTemplateList(@RequestBody TemplateQueryRequest templateQueryRequest){

        Integer pageNum = templateQueryRequest.getPageNum();
        if (pageNum == null || pageNum <= 0) {
            templateQueryRequest.setPageNum(1);
        }
        Integer pageSize = templateQueryRequest.getPageSize();
        if (pageSize == null || pageSize <= 0) {
            templateQueryRequest.setPageSize(5);
        }

        return R.ok(templateService.list(templateQueryRequest));
    }

    /**
     * 提供给外部调用
     * @param templateQueryRequest
     * @return
     */
    @PostMapping("/template/call/list")
    public R getTemplateListByCall(@RequestBody TemplateQueryRequest templateQueryRequest){
        templateQueryRequest.setPageNum(1);
        templateQueryRequest.setPageSize(999);
        return R.ok(templateService.listByCall(templateQueryRequest));
    }

    @MyLog(title = "话术管理模块",optParam = "新增话术")
    @PostMapping("/template/add")
    public R addTemplate(@RequestBody TemplateAddRequest templateAddRequest) {
        String name = templateAddRequest.getName();
        if (StringUtils.isEmpty(name)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"模板名不能为空");
        }
        String content = templateAddRequest.getContent();
        if (StringUtils.isEmpty(content)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"内容不能为空");
        }

//        String type = templateAddRequest.getType();
//        if (StringUtils.isBlank(type)) {
//            throw new BusinessException(ErrorCode.PARAMS_ERROR,"类型不能为空");
//        }

        boolean result = templateService.addTemplate(templateAddRequest);
        return R.ok(result);
    }

    @MyLog(title = "话术管理模块",optParam = "修改话术")
    @PostMapping("/template/update")
    public R updateTemplate(@RequestBody TemplateUpdateRequest templateUpdateRequest) {
        Integer id = templateUpdateRequest.getId();
        if (id == null || id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        String requestName = templateUpdateRequest.getName();
        if (StringUtils.isEmpty(requestName)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"话术名称不能为空");
        }
        templateService.updateTemplate(templateUpdateRequest);
        return R.ok(true);
    }

    @MyLog(title = "话术管理模块",optParam = "修改话术状态")
    @PostMapping("/template/update/status/true")
    public R updateStatusTRUE(@RequestBody List<Integer> ids) {

        if (ids.isEmpty()) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }

        templateService.updateStatusByIds(ids,1);

        return R.ok(true);
    }

    @MyLog(title = "话术管理模块",optParam = "修改话术状态")
    @PostMapping("/template/update/status/false")
    public R updateStatusFALSE(@RequestBody List<Integer> ids) {

        if (ids.isEmpty()) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }

        templateService.updateStatusByIds(ids,0);

        return R.ok(true);
    }

    @MyLog(title = "话术管理模块",optParam = "删除话术模板")
    @PostMapping("/template/deleteAll")
    public R deleteAllTemplate(@RequestBody TemplateDeleteRequest templateDeleteRequest){

        Integer id = templateDeleteRequest.getId();

        if (id == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }

        templateService.deleteAll(id);

        return R.ok(true);
    }

    @MyLog(title = "话术管理模块",optParam = "删除话术")
    @PostMapping("/template/delete")
    public R deleteById(@RequestBody TemplateDeleteRequest templateDeleteRequest){

        Integer id = templateDeleteRequest.getId();

        if (id == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }

        templateService.deleteById(id);

        return R.ok(true);
    }

    @GetMapping("/template/status")
    public R selectByStatus()
    {
        return R.ok(templateService.selectStatus());
    }

}
